'use strict';

var log4js = require('log4js');
var log = log4js.getLogger("tools");

var to_json = require('xmljson').to_json;
var xmlToJson = function (items) {
  return new Promise((resolve, reject) => {
    var values = {};
    to_json(items, function (err, data) {
      if (err) {
        return reject(values);
      }
      try {
        var status = data.fill.items.item[0]['$'];
        if (status.name === 'state') {
          values['state'] = parseInt(status.value);
        }
        var status = data.fill.items.item[3]['$'];
        if (status.name === 'ordermoney') {
          values['ordermoney'] = parseFloat(status.value);
        }


      } catch (err) {
        log.warn(err);
        values = {};
      }
      // console.log(values)
      resolve(values);
    });
  });
};

module.exports = {
  xmlToJson: xmlToJson
};
